<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\BlogBook;
use App\Models\BlogUser;
use App\Models\Login;
use http\Env\Response;
use Illuminate\Http\Request;
use Gregwar\Captcha\CaptchaBuilder;
use Gregwar\Captcha\PhraseBuilder;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class LoginController extends Controller
{
    /**
     * 登录
     */
    public function login(Request $request){
//        return view('admin/login');

        //自定义校验规则
        $rules = [
            'username' => 'required',
            'password' => 'required|min:6'
        ];
        $this->validate($request,$rules);

        $username = $request->input('username');
        $password = $request->input('password');
        $platform = $request->header('platform', 'pc');

        //判断账号是否存在
        $user = BlogUser::where('username',$username)->first();
        //账号不存在
        if (!$user) return error_json('账号不存在');
        //密码错误
        if (!\Hash::check($password,$user->password)) return error_json('密码错误');
        //生成token
        $token = $user->createToken($platform)->plainTextToken;
        //登录成功
        return success_json(compact('token'));
    }

    /**
     * 退出登录
     */
    public function logout(){
        return success_json('成功');
    }


    /**
     * 验证码
     */
    public function captcha($tmp){
        /*
        $phrase = new PhraseBuilder;
        //设置验证码位数
        $code = $phrase -> build(6);
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder($code,$phrase);
        //设置背景颜色
        $builder -> setBackgroundColor(220,210,230);
        $builder -> setMaxAngle(25);
        $builder -> setMaxBehindLines(0);
        $builder -> setMaxFrontLines(0);
        //可以设置图片宽高及字体
        $builder -> build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder -> getPhrase();
        //把内容存入session
        \Sessiopn::flash('code',$phrase);
        //生成图片
        header("Cache-Control:no-cache,must-revalidate");
        header("Content-Type:image/jpeg");
        $builder -> output();
        */

        $builder = new CaptchaBuilder;
        $builder->build();
        header('Content-type: image/jpeg');
        $builder->output();
    }

    /**
     * 处理用户登录
     */
    public function doLogin(Request $request){
        //接收表单提交的数据
        $input = $request->except('_token');

        //进行表单验证
        //$validator = Validator::make('需要验证的表单数据','验证规则','错误提示信息');

        //自定义验证规则
        $rule = [
            'username' => 'required|between:4,18',
            'password' => 'required|between:6,10|alpha_dash',
            'captcha' => 'required'
        ];

        $validator = validator($input,$rule);

        if ($validator->fails()){
            return redirect('admin/login')
                ->withErrors($validator)
                ->withInput();
        }

        //3.验证是否有此用户（用户名 密码 验证码）
        $user = Login::where('user_name',$input['username'])->first();
        if (!$user){
            return redirect('admin/login')->with('errors','用户名错误');
        }
        if ($input['password'] != Crypt::decrypt($user->user_pass)){
            return redirect('admin/login')->with('errors','密码错误');
        };

        //4.保存用户信息到session中
        session()->put('user',$user);

        //5.跳转到后台首页
        return redirect('admin/index');


    }

    /**
     * 加密类型
     */
    public function jiami(){
        //1.md5加密，生成一个32位的字符串 md5();
        $str = '123456';

        //2.哈希加密 Hash::make();
        $strs = '123456';
//        $hash = Hash::make($strs);
//        if (Hash::check($strs,$hash)){
//            return '密码正确';
//        } else {
//            return '密码错误';
//        }

        //3.crypt加密
        $strr = '123456';
        $crypt_str = 'eyJpdiI6InFYcnJUVHdFUUJCT1ZpdnpjcWtZYkE9PSIsInZhbHVlIjoiOEJTZjBabFpHQmRyWEMzL0pIcjRUdz09IiwibWFjIjoiODNlNTZiYWQxNDdhYWFjNTY5YmQzMWIxYjE4YjYyZTNiZGUwM2JiYzU1ZGI0ODUwYWM4ODI4NmFmZTVmYjZjYyIsInRhZyI6IiJ9';
        if (Crypt::decrypt($crypt_str) == $strr){
            return '密码正确';
        }
//        return $crypt_str;


//        return 'md5加密：'.md5($str).'<br/><br/>'.'哈希加密：'.Hash::make($strs);
    }
}
